package com.sgb.jicai.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.databinding.FragmentHomeJicaiGongfangBinding;
import com.sgb.jicai.viewmodel.HomeJiCaiChangJiaModel;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.BaseHeightViewPagerFragment;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.jicai.view.adapter.FactoryCentralizedListItemAdapter2;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.List;

/**
 * 集采首页供方列表
 */
public class HomeJiCaiChangJiaFragment extends BaseHeightViewPagerFragment {

    HomeJiCaiChangJiaModel mModel;
    FragmentHomeJicaiGongfangBinding mBind;
    private FactoryCentralizedListItemAdapter2 mAdapter;
    private JiCaiHomeListRequestBean jiCaiHomeListBean;
    private List<MenuEntity> tablayoutList;
    private MenuEntity all;

    public static HomeJiCaiChangJiaFragment newInstance() {
        HomeJiCaiChangJiaFragment fragment = new HomeJiCaiChangJiaFragment();
        Bundle args = new Bundle();
        args.putInt(BaseHeightViewPagerFragment.ARG_PARAM_POS, 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_home_jicai_gongfang, container, false);
        mModel = new ViewModelProvider(this).get(HomeJiCaiChangJiaModel.class);
        mModel.setActivity(HomeJiCaiChangJiaFragment.this);
        mBind.seeMore.setOnClickListener(this);
        mBind.seeMore.setEnabled(true);
        return mBind.getRoot();
    }

    @Override
    protected void initView() {
          if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mAdapter = new FactoryCentralizedListItemAdapter2(getActivity(), null, "1");
        mBind.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBind.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity entity, int position) {
                mModel.getH5Url(entity.getNo());
            }
        });
        if (jiCaiHomeListBean==null){
            jiCaiHomeListBean = new JiCaiHomeListRequestBean();
        }
        initListData();
        mBind.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (tablayoutList != null && tablayoutList.size() > 0) {
                    jiCaiHomeListBean.setFirstType(tablayoutList.get(position).getId());
                }
                mBind.seeMore.setEnabled(true);
                mBind.seeMore.setText("更多");
                mBind.seeMore.setTextColor(getActivity().getResources().getColor(R.color.color_secondary_blue));
                mModel.getFactoryCentralizedList(jiCaiHomeListBean, false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.see_more) {
            mModel.getFactoryCentralizedList(jiCaiHomeListBean, true);
        }
    }

    public void setRefresh() {
        mBind.seeMore.setEnabled(true);
        mBind.seeMore.setText("更多");
        mBind.seeMore.setTextColor(getActivity().getResources().getColor(R.color.color_secondary_blue));
        mModel.getFactoryCentralizedList(jiCaiHomeListBean, false);
    }

    @Override
    protected void onLazyLoad() {
        //获取供方发起tabtitle数据
        if (mModel != null) {
            mModel.getTypeDatesList("0", String.valueOf(1));
        }
    }

    public void showList(List<FactoryCentralizedItemEntity> list, boolean loadMore) {
        if (loadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            } else {
                mBind.seeMore.setEnabled(false);
                mBind.seeMore.setText("没有更多了");
                mBind.seeMore.setTextColor(getActivity().getResources().getColor(R.color.color_969696));
            }
        } else {
            if (list != null && list.size() > 0) {
                mBind.llRecyclerView.setVisibility(View.VISIBLE);
                mBind.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
                mBind.seeMore.setEnabled(true);
            } else {
                mAdapter.clearDatas();
                mBind.emptyView.setVisibility(View.VISIBLE);
                mBind.llRecyclerView.setVisibility(View.GONE);
            }
        }
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof CentralizedMiningFragment5) {
            ((CentralizedMiningFragment5) parentFragment).hideRefresh();
        }
    }


    public void showUrl(String url) {
        JiCaiBaseApi.startWeb(getActivity(), url);
    }

    //    返回列表顶部
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JiCaiEventEntity event) {
        if (event.getCode() == EventConstant.EVENT_JICAI_BACK_TO_TOP) {
            mBind.recyclerView.scrollToPosition(0);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //供方发起tabtitle数据信息
    public void setTypesFirstDatas(List<MenuEntity> data) {
        this.tablayoutList = data;
        all = new MenuEntity();
        all.setId("");
        all.setName("全部");
        data.add(0, all);
        for (MenuEntity MenuEntity : data) {
            //tablayout根据返回数据来加载title列表信息
            mBind.tabLayout.addTab(mBind.tabLayout.newTab().setText(MenuEntity.getName()));
        }
        //设置默认选中第一个数据信息
        if (jiCaiHomeListBean != null && data.size() > 0) {
            mBind.tabLayout.getTabAt(0).select();
        }
    }

    private void initListData() {
        if (jiCaiHomeListBean==null){
            jiCaiHomeListBean = new  JiCaiHomeListRequestBean();
        }
        jiCaiHomeListBean.setFirstType("");
        //获取列表信息
        mModel.getFactoryCentralizedList(jiCaiHomeListBean, false);
    }

    public void initializePop() {
        //获取供方发起tabtitle数据
        if (mModel == null)return;
        if (mModel != null) {
            mModel.getTypeDatesList("0", String.valueOf(1));
        }
        initListData();
    }


}
